#include "Ray.h"
#include <stdio.h>
using namespace std;

Ray::Ray(vec4 _origin, vec4 _dir, int _MaxDepth, int _curDepth, int _type){
	dir = normalize(_dir);	//be sure to normalize this vector
	//move the ray in the direction of travel a tiny bit in order to avoid
	//intersecting with geometry that it may have just bounced off of
	orig = _origin;
	
	updateInvDir();

	max_depth = _MaxDepth;
	theDepth = _curDepth;
	rayType = _type;
}


Ray::~Ray(){
}

void Ray::toString(){
	printf("Ray origin: (%f, %f, %f)\n", orig.x, orig.y, orig.z);
	printf("Ray direction: (%f, %f, %f)\n", dir.x, dir.y, dir.z);
	printf("Max ray depth: %d\n\n", max_depth);
}

vec4 Ray::pointAtDepth(float _d){
	return orig + dir * _d;
}

void Ray::updateInvDir(){
	//for optimized ray-tesseract intersection tests
	invDir = 1.0f / dir;
	sign[0] = (invDir.x < 0);
	sign[1] = (invDir.y < 0);
	sign[2] = (invDir.z < 0);
	sign[3] = (invDir.w < 0);
}

void Ray::transform(mat4 _rotate, vec4 _translate){
	//transform the ray origin
	orig += _translate;
	orig = _rotate * orig;
	//transform the ray direction
	dir = _rotate * dir;
	updateInvDir();
}